package com.example.Windturbine.Service.Impl;

import com.example.Windturbine.Controller.ViewObject.GraphVO.GraphNodeVO;
import com.example.Windturbine.Controller.ViewObject.GraphVO.GraphRelationNode;
import com.example.Windturbine.Controller.ViewObject.GraphVO.GraphVO;
import com.example.Windturbine.Dao.ProductDOMapper;
import com.example.Windturbine.Dao.ProductInfoDOMapper;
import com.example.Windturbine.DataObject.ProductDO;
import com.example.Windturbine.DataObject.ProductInfoDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Response.CommonResult;
import com.example.Windturbine.Service.FunctionService;
import com.example.Windturbine.Service.Model.ProductModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2021/12/18
 */
@Service
public class FunctionServiceImpl implements FunctionService {

    @Resource
    ProductDOMapper productDOMapper;

    @Resource
    ProductInfoDOMapper productInfoDOMapper;

    @Override
    public List<ProductModel> getAll() throws BusinessException {
        List<ProductDO> productDOS=new ArrayList<>();
        List<ProductModel> productModels=new ArrayList<>();
        try{
            productDOS = productDOMapper.selectAll();
            if(productDOS==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询失败！");
            }
            for(int i=0;i<productModels.size();i++){
                productModels.add(convertFromProductDO(productDOS.get(i)));
            }
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取数据出错!");
        }



        return productModels;
    }


    @Override
    public List<String> getUserFunctionInfo(Integer userid) throws BusinessException {
        List<ProductInfoDO> productInfoDOList =new ArrayList<>();
        ArrayList<String> result=new ArrayList<>();
        try{
            productInfoDOList=productInfoDOMapper.selectByUserId(userid);
            if(productInfoDOList==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询失败！");
            }
            HashSet<String> res=new HashSet<>();
            for(ProductInfoDO i:productInfoDOList){
                res.add(i.getFunctionInfo());
            }
            for(String s:res) result.add(s);
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取用户产品功能数据出错!");
        }

        return result;
    }


    @Override
    public CommonResult queryFunctionGraphInfo(String function,Integer userid) throws BusinessException {
        List<ProductInfoDO> productInfoDOList =new ArrayList<>();
        List<GraphNodeVO> graphNodeVOS=new ArrayList<>();
        List<GraphRelationNode> graphRelationNodeList=new ArrayList<>();
        GraphNodeVO parent=new GraphNodeVO();
        Integer max=0;
        try{
            productInfoDOList=productInfoDOMapper.selectByIdAndFunction(userid,function);
            if(productInfoDOList==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询失败！");
            }
            parent.setOrderInfo(function);
            parent.setLevel(0);
            parent.setNameInfo(function);
            parent.setParentorder(function);
            graphNodeVOS.add(parent);
            for(ProductInfoDO i:productInfoDOList) graphNodeVOS.add(convertFromProductInfoDO(i,function));
            for(GraphNodeVO j:graphNodeVOS){
                max=j.getLevel()>max?j.getLevel():max;
                graphRelationNodeList.add(convertFromNodeVO(j));
            }
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询用户功能图关系出错！");
        }

        GraphVO result=new GraphVO(max,graphNodeVOS,graphRelationNodeList);
        return CommonResult.create(result);
    }

    @Override
    public List<ProductInfoDO> queryFunctionListInfo(String function, Integer userid) throws BusinessException {
        try{
            return productInfoDOMapper.selectByIdAndFunction(userid,function);
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询用户功能列表数据出错！");
        }

    }


    private ProductModel convertFromProductDO(ProductDO productDO){
        if(productDO==null){
            return null;
        }
        ProductModel productModel=new ProductModel();
        BeanUtils.copyProperties(productDO,productModel);
        return productModel;
    }

    private GraphNodeVO convertFromProductInfoDO(ProductInfoDO productInfoDO, String parentName){
        GraphNodeVO graphNodeVO=new GraphNodeVO();
        BeanUtils.copyProperties(productInfoDO,graphNodeVO);
        String order=productInfoDO.getOrderInfo();
        String[] orders=order.split("\\.");
        graphNodeVO.setLevel(orders.length);
        graphNodeVO.setParentorder(parentName);
        return graphNodeVO;
    }

    private GraphRelationNode convertFromNodeVO(GraphNodeVO graphNodeVO){
        if(graphNodeVO.getLevel()==null){
            return null;
        }
        GraphRelationNode graphRelationNode=new GraphRelationNode();
        graphRelationNode.setParent(graphNodeVO.getParentorder());
        graphRelationNode.setChild(graphNodeVO.getId()+"");
        graphRelationNode.setName(graphNodeVO.getLevel()+"级零件");
        graphRelationNode.setDes(graphNodeVO.getParentorder()+"功能结点");
        return graphRelationNode;
    }

    private ProductModel convertFromDOAndInfoDO(ProductDO productDO,List<ProductInfoDO> productInfoDOList){
        if(productInfoDOList==null||productDO==null){
            return null;
        }
        ProductModel productModel=new ProductModel();
        BeanUtils.copyProperties(productDO,productModel);
        productModel.setProductInfoDOList(productInfoDOList);
        return productModel;
    }


}
